package com.sunnyebuy.merchant.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.ognl.OgnlValueStack;
import com.sunnyebuy.merchant.SystemConfig;

/**
 * <p>
 * Title: MerchantLoginInterceptor.java
 * </p>
 * 
 * <p>
 * Description: com.sunnyebuy.merchant.interceptor
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2008-2011 All rights reserved.
 * </p>
 * 
 * <p>
 * Created: 2011-12-28 上午11:26:39
 * </p>
 * 
 * @author Wenzhong Gu
 * 
 * @version 1.0
 */
public class MerchantLoginInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = -2969714225211725733L;

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		String merchantId = (String) actionInvocation.getInvocationContext().getSession()
				.get(SystemConfig.MERCHANT_ID_SESSION_NAME);
		if (merchantId == null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String loginRedirectUrl = request.getServletPath();
			String queryString = request.getQueryString();
			if (StringUtils.isNotEmpty(queryString)) {
				loginRedirectUrl += "?" + queryString;
			}
			OgnlValueStack ognlValueStack = (OgnlValueStack) request.getAttribute("struts.valueStack");
			ognlValueStack.set("loginRedirectUrl", loginRedirectUrl);
			return "login";
		}
		return actionInvocation.invoke();
	}
}